//电话
var NYTele = function (options) {
	$.extend(this, {
		outSideLineNo: '9',
		ocxControl: null,
		enableRecord: 1,
		workPath: '{ftp_path}'
	}, options || {});
	
	if (!this.ocxControl) {
		return alert('控件连接不正常，请检查');
	}
	
	// 属性赋值
	this.connected = false;
	this.callNo = '';
	this.callType = 0;
	this.isCalling= false;
	this.isMute = 0;
	this.isHold = 0;
	
	// 控件初始化
	this.ocxControl.InitTeleDevice();
	this.ocxControl.OutsideLineNo = this.outSideLineNo;
	this.ocxControl.EnableRecord = this.enableRecord;
	this.ocxControl.UserId = JYUser.userId;
	this.ocxControl.UserName = JYUser.userName;
	this.ocxControl.UnitId = JYUser.unitId;
	this.connected = this.ocxControl.DeviceConnected;
	
	this.onHangUp = null;
	this.onCallOut = null;
	this.onCallIn = null;
};
NYTele.prototype.callOut = function (teleNo) {
	if (!this.connected) {
		this.ocxControl.InitTeleDevice();
		this.ocxControl.OutsideLineNo = this.outSideLineNo;
		this.connected = this.ocxControl.DeviceConnected;
	}
	if (this.connected && this.ocxControl.DeviceConnected) {
		this.callNo = teleNo;
		this.ocxControl.CallOut(this.callNo);
	} else {
		this.connected = false;
		alert('设备连接不正常，请检查电话与电脑的链接。');
	}
};
NYTele.prototype.hangUp = function () {
	this.ocxControl.HangUp();
};
NYTele.prototype.answer = function () {
	this.ocxControl.Answer();
};
NYTele.prototype.hold = function (on) {
	this.ocxControl.Hold(on);
};
NYTele.prototype.mute = function (on) {
	this.ocxControl.Mute(on);
};
NYTele.prototype.bell = function (on) {
	this.ocxControl.Bell(on);
};
NYTele.prototype.login = function () {
	this.ocxControl.LoginYuyue();
};
NYTele.prototype.getRecordFileName = function () {
	return this.ocxControl.LocalRecordFileName;
};
NYTele.prototype.saveCallRecord = function () {
	// 此时必须使得全部不可用，在处理完本业务逻辑后，才能再次发起或者接电话
	var startTime = this.ocxControl.StartTime;
	var endTime = this.ocxControl.EndTime;
	var duration = this.ocxControl.Duration;
	var fileName = this.ocxControl.FtpRecordFileName;
	var callType = this.callType;
	var telNo = this.callNo;
	var data = {startTime: startTime, endTime: endTime, duration: duration, fileName: fileName, 
			callType: callType, telNo: telNo};
	var self = this;
	$.post('index.php?c=tele&a=saveRecord', data, function (rsp) {
		if (rsp.code == 1) {
			JYCsm.messager.succ(rsp.msg);
			// ftp文件上传
			self.uploadRecordFile();
		} else if (rsp.code){
			JYCsm.messager.error(rsp.msg);
		} else {
			
		}
	}, 'json');
};
NYTele.prototype.uploadRecordFile = function () {
	this.ocxControl.FtpPut(this.ocxControl.LocalRecordFileName);
	JYCsm.messager.succ('文件上传成功');
};


// ftp
var NYFtp = function (options) {
	$.extend(this,
	{host: '{ftp_ip}',
	port: 21,
	userName: '{ftp_user}',
	password: '{ftp_pwd}',
	passive: 1,
	dir: 'csmftp',
	ocxControl: null,
	workPath: '{ftp_path}'}, options||{});
	
	if (!this.ocxControl) {
		return alert('控件连接不正常，请检查');
	}
	
	// 控件初始化
	this.ocxControl.FtpHost = this.host;
	this.ocxControl.FtpPort = this.port;
	this.ocxControl.FtpPassive = this.passive;
	this.ocxControl.FtpUserName = this.userName;
	this.ocxControl.FtpPassword = this.password;
	this.ocxControl.FtpDir = this.dir;
	this.ocxControl.WorkPath = this.workPath;
};
NYFtp.prototype.put = function (localFileName) {
	this.ocxControl.FtpPut(localFileName);
};
NYFtp.prototype.putDir = function (dir) {
	this.ocxControl.FtpPutDir(dir);
};